import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart' as dio;
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:path_provider/path_provider.dart';
import 'package:situation/app/theme/gaps.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:video_thumbnail/video_thumbnail.dart';

class UtilMk {
  static KeyboardActionsConfig getKeyboardActionsConfig(
    BuildContext context,
    List<FocusNode> list,
  ) {
    return KeyboardActionsConfig(
      // keyboardBarColor: MkThemeUtils.getKeyboardActionsColor(context),
      actions: List.generate(
        list.length,
        (i) => KeyboardActionsItem(
          focusNode: list[i],
          toolbarButtons: [
            (node) {
              return GestureDetector(
                onTap: () => node.unfocus(),
                child: Padding(
                  padding: const EdgeInsets.only(right: 16.0),
                  child: Text('关闭'),
                ),
              );
            },
          ],
        ),
      ),
    );
  }

  static void openBottomSheet(double height, String title, Widget child) {
    Get.bottomSheet(
      Container(
        width: Get.width,
        height: height,
        margin: EdgeInsets.symmetric(horizontal: 12),
        child: Column(
          children: [
            Semantics(
              button: true,
              label: '返回',
              child: Container(
                key: const Key('widget_move_modal_bottom_sheet'),
                margin: EdgeInsets.all(12),
                height: 4,
                width: 30,
                decoration: BoxDecoration(
                  color: const Color(0xFF2B3034),
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
            ),
            title != ''
                ? Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                      child: Text(
                        title,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                    // Container(
                    //   width: 26,
                    //   height: 26,
                    //   decoration: BoxDecoration(
                    //       color: Color(0xFFEEEEEE),
                    //       borderRadius: BorderRadius.all(Radius.circular(26))),
                    //   child: GestureDetector(
                    //     onTap: () {
                    //       Get.back();
                    //     },
                    //     child: Icon(
                    //       Icons.close,
                    //       color: Colors.black,
                    //       size: 14,
                    //     ),
                    //   ),
                    // )
                  ],
                )
                : SizedBox(),
            Gaps.vGap8,
            child,
          ],
        ),
      ),
      isScrollControlled: true,
      barrierColor: Colors.black26,
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(32),
          topRight: Radius.circular(32),
        ),
      ),
    );
  }

  // 获取当前时间戳 秒
  static int getCurrentTimeStamp() {
    return DateTime.now().millisecondsSinceEpoch ~/ 1000;
  }

  static Future<String> getThumbnal(videoPath) async {
    final Directory extDir = await getApplicationDocumentsDirectory();
    final String filePath =
        '${extDir.path}/' +
        getCurrentTimeStamp().toString() +
        '.png'; // 存储缩略图的路径
    final thumbnailData = await VideoThumbnail.thumbnailData(
      video: videoPath,
      imageFormat: ImageFormat.PNG, // 输出格式，可以是JPEG或PNG等
      maxWidth: 1280, // 指定最大宽度，可选参数，不设置则默认全尺寸图片
      quality: 25, // 图片质量，可选参数，不设置则默认100%质量，JPEG有效，PNG无效。
    );
    final File file = await File(
      filePath,
    ).writeAsBytes(thumbnailData!); // 写入文件并保存路径
    return file.path;
  }

  static String formatDateTime(int timestamp) {
    final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    final String formattedDate = '${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')} ${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}';
    return formattedDate;
  }


  static String formatDateTimeYMDHIS(int timestamp) {
    final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    final String formattedDate = '${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')} ${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}:${dateTime.second.toString().padLeft(2, '0')}';
    return formattedDate;
  }

  static String formatDateTimeGetMonth(int timestamp) {
    final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    return dateTime.month.toString().padLeft(2, '0');
  }

  static String formatDateTimeGetDay(int timestamp) {
    final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    return dateTime.day.toString().padLeft(2, '0');
  }

    static String formatDateTimeGetYear(int timestamp) {
    final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    return dateTime.year.toString();
  }


    static String formatDateTimeGetHis(int timestamp) {
    final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    return '${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}:${dateTime.second.toString().padLeft(2, '0')}';
  }


  static String getFileType(String filePath) {
    // 获取文件扩展名
    String extension = filePath.split('.').last.toLowerCase();

    // 定义图片和视频的常见扩展名列表
    List<String> imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'webp'];
    List<String> videoExtensions = ['mp4', 'mov', 'avi', 'mkv', 'flv'];

    // 判断文件类型
    if (imageExtensions.contains(extension)) {
      return 'img';
    } else if (videoExtensions.contains(extension)) {
      return 'Video';
    } else {
      return 'Unknown'; // 未知类型或不支持的文件格式
    }
  }







}
